package com.design.singleton;
//懒汉线程安全
public class Singleton {
    //保证可见性、禁止指令重排
    private volatile static Singleton uniqueInstance;
    private Singleton() {
    }
    public static Singleton getUniqueInstance() {
        //为什么要 double-check？
        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    //是给singleton分配内存空间
                    //开始调用Singleton的构造函数等，来初始化singleton
                    //将singleton对象指向分配的内存空间（执行完这步singleton就不是null了）
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
}
